package com.rainfull.ui;

import java.util.HashMap;

import com.rainfull.core.RNetworkProcess;
import com.rainfull.core.RUserInfo;
import com.rainfull.ui.R;
import com.rainfull.ui.RUIAccount.RUIRegLogin;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.ProgressDialog;
import android.content.Intent;

public class RUIAccountReg extends Activity implements OnClickListener
{  
	private String m_strTip = "";
	private ProgressDialog m_progressDialog = null;
	private Intent m_inIntent = null;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {    	
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_reg);        
        
        m_inIntent = getIntent();  
        Button buttonOk = (Button) findViewById(R.id.acount_reg_btn_ok);
        buttonOk.setOnClickListener(this);

    	Button buttonCancle = (Button) findViewById(R.id.acount_reg_btn_cancle);
    	buttonCancle.setOnClickListener(this);
    	
    }
	public void onClick(View v) 
	{
		int nId = v.getId();
		switch(nId)
		{
			case R.id.acount_reg_btn_cancle:
			{
				finish();
	    		return;
			}
			case R.id.acount_reg_btn_ok:			
			{
				final EditText editUserName =(EditText)findViewById(R.id.acount_reg_user_name);	
				final EditText editPwd =(EditText)findViewById(R.id.acount_reg_pwd);
				final EditText editPwd2 =(EditText)findViewById(R.id.acount_reg_pwd2);
				if (editUserName.getText().toString().length() == 0 ||
						editPwd.getText().toString().length() == 0 ||
						editPwd2.getText().toString().length() == 0)
				{
					Toast.makeText(this, "请填写完整的注册信息！", Toast.LENGTH_LONG).show();
					return;
				}
				else if (editPwd.getText().toString().equals(editPwd2.getText().toString()) == false)					
				{
					Toast.makeText(this, "两次输入的密码不相同!", Toast.LENGTH_LONG).show();
					return;
				}
				m_strTip = "正在注册，请稍后...";
				m_progressDialog = ProgressDialog.show(this, "", m_strTip, true);
				HashMap<String, Object> mapRegInfo = new HashMap<String, Object>();
				mapRegInfo.put("userName", editUserName.getText().toString());
				mapRegInfo.put("pwd", editPwd.getText().toString());
				new RUIReg().execute(mapRegInfo);				
				break;
			}
		}
	}
	//登陆注册接口
	public class RUIReg extends AsyncTask
	{
		@Override
		protected Object doInBackground(Object... param) 
		{
			HashMap<String, Object> mapRegInfo = (HashMap<String, Object>)param[0];			   
			
			RUserInfo userInfo = null;
			String strUserName = mapRegInfo.get("userName").toString();
			String strPwd = mapRegInfo.get("pwd").toString();
			userInfo = RNetworkProcess.userReg(strUserName, strPwd);			
			mapRegInfo.put("userInfo", userInfo);			
			return mapRegInfo;
		}
		protected void onPostExecute(Object result) 
		{  
			if (m_progressDialog != null)
				m_progressDialog.dismiss();
			HashMap<String, Object> mapRegInfo = (HashMap<String, Object>)result;			   
			RUserInfo userInfo = (RUserInfo)mapRegInfo.get("userInfo");		 
			if (userInfo.getUserName().length() == 0)
			{
				int nType = Integer.parseInt(mapRegInfo.get("type").toString());
				Toast.makeText(RUIAccountReg.this, "注册失败，请重试!", Toast.LENGTH_LONG).show();				
			}
			else
			{
				m_inIntent.putExtra("left_count", userInfo.getLeftCount());
				m_inIntent.putExtra("score", userInfo.getScore());
				m_inIntent.putExtra("token", userInfo.getToken());
				m_inIntent.putExtra("user_name", userInfo.getUserName());
				setResult(RESULT_OK, m_inIntent);
				finish();
	    		return;
			}
		}
	}
}
